// pages/memberManage/memberManage.js
import http from '../../utils/http.js';
import Dialog from '@vant/weapp/dialog/dialog';

import { GetStoreMemberMemberlistApi,memberDeleteApi } from '../../utils/newApi.js';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    isAdmin:null,
    storeInfo:{},
    memberList :[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options")
    this.setData({
      id: options.id,
      isAdmin: options.admin
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.GetMemberList()
  },

  GetMemberList() {
    let that = this
    // http.post('/api/store/v1/user/list', {
    //   id: that.data.id
    // }).then(res => {
    //   if (res.code === 200) {
    //     if (res.data.length > 0) {
    //       that.setData({
    //         memberList: res.data
    //       })
    //     }
    //   } else {
    //     wx.showToast({
    //       title: res.message,
    //       icon:'none'
    //     })
    //   }
    // })
    let data={
      store_id:that.data.id,
      page:1,
      pageSize:20,
      pagination:true

    }
    GetStoreMemberMemberlistApi(data).then(res=>{
      that.setData({
        memberList: res.data.List
      })
    })
  },

  JumpAddMember() {
    console.log(this.data.isAdmin,"this.data.isAdmin")
    if (this.data.isAdmin==1) {
      wx.navigateTo({
        url: '/pages/AddMember/AddMember?businessId=' + this.data.id
      })
    } else {
      wx.showToast({
        title: '非管理员，暂无权限', 
        icon:'none'
      })
    }
  },

  BindMemberRemove(e) {
    Dialog.confirm({
      title: '提示',
      message: '您确定要删除这个成员嘛',
    }).then(() => {
      let that = this
      memberDeleteApi({id: e.currentTarget.dataset.id,}).then(res => {
        if (res.code === 0) {
          wx.showToast({
            title: '删除成功',
            icon:'none'
          })
          that.GetMemberList()
        } else {
          wx.showToast({
            title: res.message,
            icon:'none'
          })
        }
      })
      // http.post('/api/store/v1/user/remove', {
      //   id: e.currentTarget.dataset.id,
      //   businessId:e.currentTarget.dataset.businessid
      // }).then(res => {
      //   if (res.code === 200) {
      //     wx.showToast({
      //       title: '删除成功',
      //       icon:'none'
      //     })
      //     that.GetMemberList()
      //   } else {
      //     wx.showToast({
      //       title: res.message,
      //       icon:'none'
      //     })
      //   }
      // })
    })
  },
  // 修改
  JumpModify(e) {
    if(this.data.isAdmin==1){
      const { info } = e.currentTarget.dataset
      info.businessId = this.data.id
      info.storePublicityImgs = []
      // info.adminMemberAvatar = ''
      console.log(info,"infoinfoinfoinfo")
      // let data=JSON.stringify(info)
      // let data1=JSON.parse(data)
      // console.log(data1,"data1")

      wx.navigateTo({
        url: '/pages/AddMember/AddMember?info='
        + encodeURIComponent(JSON.stringify(info))
      })
    }else{
      wx.showToast({
        title: '非管理员，暂无权限', 
        icon:'none'
      })
    }
    
  }
})